<?php
class Address extends CS_Controller
{
    public function _init()
    {
        $this->load->helper(array('common', 'validation'));
        $this->load->model('tourism_delivery_address_model', 'tourism_delivery_address');
        $this->load->model('region_model', 'region');
    }

    /**
     * 地址
     */
    public function addressList()
    {
        $this->data['addressList'] = $this->tourism_delivery_address->findOne(['uid' => $this->uid]);
        $this->load->view('home/address/addressList', $this->data);
    }

    /**
     * 地址添加
     */
    public function addressInfoAdd()
    {
        $this->data['region'] = $this->region->getChinaRegion(array('region_type' => 1));
        $this->load->view('home/address/addressInfoAdd', $this->data);
    }

    /**
     * 地址提交
     */
    public function addPost()
    {
        $postData = $this->input->post();
        $data = $this->_validate($postData);
        $data['is_default'] = 1;
        $result = $this->tourism_delivery_address->insert($data);
        if ($result) {
            $this->success('home/address/addressList','','新增成功');
        }  else {
            $this->error('home/address/addressList','','新增失败');
        }
    }

    /**
     * 地址编辑
     */
    public function addressInfoEdit($id)
    {
        $address = $this->tourism_delivery_address->findOne(['address_id' => $id]);
        $this->data['provinces'] = $this->region->getChinaRegion(array('region_type' => 1));
        if (0 >= $address->num_rows()) {
            $this->alertJumpPre('地址有误');
        }
        $address = $address->row();

        $this->data['cities'] = $this->region->findByType($address->province_id, 2); //城市
        $this->data['districts'] = $this->region->findByType($address->city_id, 3);//区域

        $this->data['addressInfo'] = $address;
        $this->load->view('home/address/addressInfoEdit', $this->data);
    }

    /**
     * 地址更新
     */
    public function editPost()
    {
        $postData = $this->input->post();
        $data = $this->_validate($postData);
        $result = $this->tourism_delivery_address->updateAddress($data);
        if ($result) {
            $this->success('home/address/addressList','','编辑成功');
        } else {
            $this->error('home/address/addressList','','编辑失败');
        }
    }

    /**
     * 地址删除
     */
    public function addressInfoDelete($id)
    {
        $result = $this->tourism_delivery_address->delete(['address_id' => $id, 'uid' => $this->uid]);
        if ($result) {
            $this->success('home/address/addressList','','删除成功');
        } else {
            $this->error('home/address/addressList','','删除失败');
        }
    }

    public function setDefaultAddress($id)
    {
        $non = $this->tourism_delivery_address->setNonDefault($this->uid);
        $result = $this->tourism_delivery_address->setDefault($id);
        if ($result) {
            $this->success('home/address/addressList','','操作成功');
        } else {
            $this->error('home/address/addressList','','操作失败');
        }
    }

    /**
     * 地址验证
     */
    private function _validate($postData)
    {
        if ( empty($postData['receiver_name']) ) {
            $this->alertJumpPre('请填写收件人姓名');
        }
        
        if ( ! valid_mobile($postData['tel']) ) {
            $this->alertJumpPre('请填写正确的手机号码');
        }

        if ( ! validateZipCode($postData['code']) ) {
            $this->alertJumpPre('请填写正确的邮政编码');
        }

        if ( empty($postData['detailed']) ) {
            $this->alertJumpPre('详细地址不能为空');
        }
        $regionIds = array($postData['province_id'], $postData['city_id'], $postData['district_id']);

        $result = $this->region->findRegionIds($regionIds);
        if ( $result->num_rows() != 3 ) {
            $this->alertJumpPre('请选择所在区域');
        } else {
            $flag = 0;
            foreach ($result->result() as $key => $value) {
                if ( $value->region_type == 1 ) {
                    $postData['province_name'] = $value->region_name;
                    $flag += 1;
                } elseif ( $value->region_type == 2 ) {
                    $postData['city_name'] = $value->region_name;
                    $flag += 2;
                } elseif ( $value->region_type == 3 ) {
                    $postData['district_name'] = $value->region_name;
                    $flag += 3;
                }
            }
            if ( $flag != 6 ) {
                $this->alertJumpPre('所在区域选择有误');
            }
        }
        $postData['uid'] = $this->uid;
        return $postData;
    }

    /**
     * ajax刷新地址
     */
    public function ajaxRegion()
    {
        $type = $this->input->post('type');
        $id = $this->input->post('id');
        $data['region'] = $this->region->findByType($id, $type);

        $this->ajaxJson([
            'status' => true,
            'html'   => $this->load->view('home/address/ajaxRegion', $data,  true)
        ]);
    }
}











